package com.qire.manhua.model.viewModel.impl;

import android.content.res.ColorStateList;
import android.graphics.Color;
import android.view.View;

import com.qire.antscore.annotation.PropertyObserve;
import com.qire.common.basic.ColorUtil;
import com.qire.common.basic.StringUtil;
import com.qire.common.model.entity.UserEntity;
import com.qire.common.support.base.BaseViewModel;
import com.qire.common.utils.ToastHelper;
import com.qire.manhua.R;
import com.qire.manhua.model.entity.BulletScreenInfo;
import com.qire.manhua.model.viewModel.IBulletScreenDialogViewModel;

public class BulletScreenDialogViewModel extends BaseViewModel<IBulletScreenDialogViewModel> {

    private String fontColorTag = BulletScreenInfo.WhiteColorTag;

    public BulletScreenDialogViewModel() {
        set("fontSizeNumber", 1);
        setShowTextAttrSetPanel(false);
        setFontColorTag(fontColorTag);
    }

    @PropertyObserve(name = "contentText")
    public void contentTextChange(String contentText) {
        boolean invalidContentText = invalidContentText(contentText);

        set("sendBtnClickable", !invalidContentText);
        set("sendBtnTextColor", ColorStateList.valueOf(invalidContentText ? Color.GRAY : Color.WHITE));
    }

    public void setContentText(String contentText) {
        if(contentText == null) {
            contentText = "";
        }

        if(contentText.length() > 20) {
            ToastHelper.showToast("内容不能超过20个字");
            contentText = contentText.substring(0,20);
        }

        set("contentText", contentText);
        set("contentTextSelection", contentText.length());
    }

    private boolean invalidContentText(String contentText){
        return StringUtil.isEmpty(contentText) || contentText.length()>20;
    }

    public void setShowTextAttrSetPanel(boolean showTextAttrSetPanel) {
        set("iconTextSet", showTextAttrSetPanel ? R.drawable.app_bullet_dialog_icon_aa_b : R.drawable.app_bullet_dialog_icon_aa_a);
        set("textAttrSetPanelVisible", showTextAttrSetPanel ? View.VISIBLE : View.GONE);
        set("purchaseMemberPanelVisible", getPurchaseMemberPanelVisible(showTextAttrSetPanel));
    }

    private int getPurchaseMemberPanelVisible(boolean showTextAttrSetPanel) {
        int visible = View.VISIBLE;

        if(!UserEntity.self.isLogin()) {
            return View.VISIBLE;
        }

        if(UserEntity.self.isVip() && showTextAttrSetPanel) {
            visible = View.INVISIBLE;
        }
        if(!showTextAttrSetPanel) {
            visible = View.INVISIBLE;
        }
        return visible;
    }

    public void setFontColorTag(String fontColorTag) {
        set("fontColor", toFontColor(fontColorTag));
        this.fontColorTag = fontColorTag;
    }

    private String toFontColor(String fontColorTag) {
        if(BulletScreenInfo.WhiteColorTag.equals(fontColorTag)) {
            return "#aeaeae";
        }

        Integer[] colors = BulletScreenInfo.queryFontColorByTag(fontColorTag);

        if(colors == null) {
            return "#aeaeae";
        }

        return ColorUtil.RGB.from(colors[0]);
    }

    public BulletScreenInfo bulletScreenInfo() {
        String contentText = getToValue("contentText");
        int fontSizeNumber = getToValue("fontSizeNumber");
        return StringUtil.isEmpty(contentText) ? null : new BulletScreenInfo(contentText, fontSizeNumber + 1, fontColorTag);
    }

}
